Understanding :nth-child() vs :nth-last-child() in CSS
The :nth-child() and :nth-last-child() pseudo-classes in CSS are used to select elements based on their position among siblings, but they count from opposite ends.
:nth-child(n) – Selects the nth child from the start (first child is 1).
:nth-last-child(n) – Selects the nth child from the end (last child is 1).
Both can use formulas like 2n, 2n+1, or 3n+2 for selecting patterns of elements.
In this example, :nth-child(2n) highlights items 2, 4, and 6 from the start with a light blue background. :nth-last-child(2n) makes items 2, 4, and 6 from the end bold, demonstrating the difference in counting direction.
Use :nth-child() when targeting elements from the beginning of the container.
Use :nth-last-child() when targeting elements from the end.
Combine with other pseudo-classes like :not() or :first-child for precise selections.
Test with dynamic content as adding/removing elements may affect which items are selected.
You're styling a list of user avatars and want to highlight every third one starting from the first — but when you add a profile banner above the list, the styling breaks. What’s likely going wrong, and how would you fix it?
You used :nth-child(2n) to style alternating rows in a table, but the last row isn’t getting the right background. Why might that be, and how would you verify your selector is working correctly?
A teammate says :nth-last-child(1) and :last-child do the same thing — is that true? Show me an example where they behave differently.
Our dynamic comment thread sometimes shows a 'New!' badge on the wrong comment after a user deletes one. We’re using :nth-child(1) to target the first comment — what’s the bug, and how would you fix it without changing the HTML structure?
A component renders a list of items where the first and last items have special padding, but the padding breaks when the list is filtered client-side. How would you debug and resolve this using CSS selectors?
We’re using :nth-last-child(-n+2) to style the last two items in a feed, but when a loading skeleton appears, the styles apply incorrectly. How do you handle this without adding extra classes?
You’re building a reusable card grid component that needs to style the last two items differently for visual balance, but the component is used in multiple contexts — some with dynamic content, some with placeholders. How would you design the CSS to be robust across all cases without relying on JS?
A legacy UI uses :nth-child() heavily for layout, but now we’re migrating to a server-rendered system where the number of siblings varies by user role. How do you refactor the CSS to avoid brittle styling and reduce regressions?
In a high-traffic dashboard, we’re seeing layout shifts when users toggle filters that change the number of list items. How would you audit and optimize the CSS selectors to minimize reflows and ensure visual stability?
We’re standardizing a design system across 15+ products, and some teams rely on :nth-child() for layout while others use :nth-last-child() — this is causing inconsistent behavior in shared components. How would you architect a solution that enforces predictability without forcing teams to rewrite all their CSS?
A legacy component uses :nth-last-child() to style footer actions, but now we’re adding A/B test variants that inject promotional banners conditionally. How do you decouple styling from structural assumptions at the architecture level to support long-term experimentation?
Our CSS-in-JS library allows dynamic nth-child selectors based on props, but we’re seeing performance degradation in large lists. How would you evaluate whether to replace dynamic nth-child logic with static classes or semantic markup, and what tradeoffs would you present to engineering leadership?